package com.nashuju.web.controller.sw;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import org.durcframework.core.GridResult;
import org.durcframework.core.MessageResult;
import org.durcframework.core.controller.CrudController;

import com.alibaba.druid.filter.AutoLoad;
import com.nashuju.web.entity.sw.RUser;
import com.nashuju.web.entity.sw.RUserSch;
import com.nashuju.web.service.sw.RUserService;
import com.nashuju.web.service.user.UserService;
import com.nashuju.web.util.PasswordHash;
import com.nashuju.web.util.PasswordUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class RUserController extends
		CrudController<RUser, RUserService> {

	@Autowired 
	UserService userService;
		//新增记录
	@RequestMapping("/addSUser.do")
	public @ResponseBody
	MessageResult addRUser(RUser entity) throws NoSuchAlgorithmException, InvalidKeySpecException {
		String newpwd = PasswordHash.createHash(entity.getPassword());
		entity.setPassword(newpwd);
		try {
			userService.getByUserName(entity.getUsername());
			return error("用户名已存在");
		} catch (Exception e) {
			return this.save(entity);
		}
			
	}

	//删除记录
	@RequestMapping("/delSUser.do")
	public @ResponseBody
	MessageResult delRUser(RUser entity) {
		return this.delete(entity);
	}
	
	//修改记录
	@RequestMapping("/updateSUser.do")
	public @ResponseBody
	MessageResult updateRUser(RUser entity) throws NoSuchAlgorithmException, InvalidKeySpecException {
		String newpwd = PasswordHash.createHash(entity.getPassword());
		entity.setPassword(newpwd);
		return this.update(entity);
	}

	//条件查询分页操作
	@RequestMapping("/listSUser.do")
	public @ResponseBody
	GridResult listRUser(RUserSch searchEntitySch) {
		return this.query(searchEntitySch);
	}

	
	//条件查询并返回所有记录
	@RequestMapping("/listAllSUser.do")
    public @ResponseBody Object listAllRUser(RUserSch searchEntitySch) {
        return this.queryAll(searchEntitySch);
    }

    //获取详细信息
	@RequestMapping("/getSUserById.do")
    public @ResponseBody RUser getRUserById(RUser entity) {
        return this.getService().get(entity.getId());
    }

 	

}